package controllers;

import dto.Language;
import dto.RelatedTopicDto;
import dto.TopicDto;
import models.Config;
import models.Constants;
import models.TinTuc;
import models.Topic;
import models.test.Product;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.Play;
import play.i18n.Lang;
import play.mvc.Before;
import play.mvc.Controller;
import services.ConfigService;
import services.MenuService;
import services.TopicService;

import java.io.ByteArrayInputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class User extends Controller{
    @Before
    static void setMenu() {
        Lang.set("vn");
        
        List[] productMenuItems = MenuService.getProductMenuItems();

        Play.configuration.put("productMenus", productMenuItems);

        Play.configuration.put("appMenus", MenuService.getAppMenuItems());
    }

    public static void home() {
        if (StringUtils.isEmpty(Lang.get())) {
            Lang.set("vn");
        }

        //@TODO: need order by date
        List<TinTuc> allNews = TinTuc.all(TinTuc.class).filter("language", Lang.get()).fetch(5);
        render(allNews);
    }

    public static void topic(String code) {
        Topic topic = Topic.all().filter("code", code).filter("language", Lang.get()).get();
        
        if (topic==null) {
            flash.error("Doesn't exist topic with code=" + code + " in language=" + Lang.get());
            renderTemplate("User/error.html");
        }
        
        System.out.println("topic="+topic);//System.out.println("topic, name=" + topic.name);
        List<RelatedTopicDto> relatedTopics = TopicService.getRelatedTopicDto(topic);
        //System.out.println(relatedTopics.size());
        render(topic, relatedTopics);
    }

    /**
     *
     * @param topicId
     * @param type  : 1 = relatedPhoto, 2 = largePhoto
     */
    public static void getTopicImage(Long topicId, int type) {
        if (topicId==null) return;

        Topic t = Topic.findById(topicId);
        if (t==null) {
            return;
        }

        response.setContentTypeIfNotSet("jpg");
        if (type == 1 && t.relatedPhoto!=null) {
            renderBinary(new ByteArrayInputStream(t.relatedPhoto));
        }

        if (type == 2 && t.largePhoto!=null) {
            renderBinary(new ByteArrayInputStream(t.largePhoto));
        }
    }
    public static void inputNews(){
        render();

    }
    public static void allNews() {
        List<TinTuc> allNews = TinTuc.all(TinTuc.class).filter("language", Lang.get()).fetch();
        System.out.println("All News:"+allNews);
        render(allNews);
    }



    public static void doaddnews(String title, String brief, String content, byte[] photo){
        Date dateModified = Calendar.getInstance().getTime();
        TinTuc tintuc = new TinTuc(dateModified,title,brief,content);

        //tintuc.setPhoto(Admin.getSampleImage());

        tintuc.save();
        redirect("User.allNews");
    }

    public static void aboutUs_vn() {
        render();
    }

    public static void aboutUs_en() {
        render();
    }

    public static void contact_vn() {
        render();
    }
    public static void contact_en() {
        render();
    }

    public static void changeLanguage(String langCode) {
        System.out.println("will change to " +  langCode);
        Lang.change(langCode);

        redirect("User.aboutUs_"+langCode);
    }
    
    public static  void detailsNews(Long id){
        TinTuc allInfoNews = TinTuc.getByKey(TinTuc.class, id);
        System.out.println("One news: " + allInfoNews );
        render(allInfoNews);
    }

    public static void incoming() {
        render();
    }
}
